package com.pingan.internal.point.shopping.controller;

import com.alibaba.fastjson.JSON;
import com.pingan.internal.point.shopping.common.utils.FileUtils;
import com.pingan.internal.point.shopping.controller.form.GoodsForm;
import com.pingan.internal.point.shopping.controller.form.GoodsOrderForm;
import com.pingan.internal.point.shopping.controller.response.DataPageList;
import com.pingan.internal.point.shopping.controller.response.ResponseData;
import com.pingan.internal.point.shopping.service.GoodsService;
import com.pingan.internal.point.shopping.vo.GoodsVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.UUID;

@Controller
@RequestMapping("goods")
public class GoodsController {
    private static final Logger LOGGER = LoggerFactory.getLogger(GoodsController.class);

    @Resource
    private GoodsService goodsService;

    @Resource
    private ResourceLoader resourceLoader;

    @Value("${point.goods.upload.path}")
    private String path;

    @PostMapping("list")
    public @ResponseBody
    ResponseData list(@RequestBody GoodsForm form){
        LOGGER.info("GoodsController_list_form:{}", JSON.toJSONString(form));
        DataPageList<GoodsVo> res =  goodsService.list(form);
        LOGGER.info("GoodsController_list_end");
        return ResponseData.success(res);
    }

    /**
     * 显示单张图片
     * @return
     */
    @GetMapping("show/{fileName}")
    public void showPhotos(HttpServletResponse response, @PathVariable String fileName){
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");
        ServletOutputStream out =null;
        InputStream is =null;
        try {
            org.springframework.core.io.Resource res =  resourceLoader.getResource("file:" + path + fileName);
            out = response.getOutputStream();
            byte[] b =new byte[1024];
            int len;
            is = res.getInputStream();
            while ((len = is.read(b)) != -1){
                out.write(b,0,len);
            }
        } catch (Exception e) {
            return;
        }finally {
            closeStream(out, is);
        }
    }

    private void closeStream(ServletOutputStream out, InputStream is) {
        if(is !=null){
            try{
                is.close();
            }catch (Exception e){
                LOGGER.error("图片显示关闭输入流失败:",e);
            }
        }
        if(out !=null){
            try{
                out.close();
            }catch (Exception e){
                LOGGER.error("图片显示关闭输出流失败:",e);
            }
        }
    }


}
